package com.example.controller;

import com.example.pojo.Dept;
import com.example.pojo.Result;
import com.example.service.DeptService;
import com.example.service.impl.DeptServiceimpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result findAll(){
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }
    // 删除部门
    @DeleteMapping
    public Result DeleteId(Integer id){
        log.info("删除部门id为{}",id);
        deptService.DeleteId(id);
        return Result.success();
    }

    // 新增部门
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    // 根据id查询部门
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询部门{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    // 修改部门
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门：{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
